<template>
  <!-- card -->
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <!-- form表单 -->
      <el-form
        :inline="true"
        :model="queryParams"
        ref="resourceForm"
        class="demo-form-inline"
      >
        <el-form-item label="资源名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="资源名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="资源路径" prop="url">
          <el-input
            v-model="queryParams.url"
            placeholder="资源路径"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="资源分类" prop="categoryId">
          <el-select
            v-model="queryParams.categoryId"
            placeholder="资源分类"
            clearable
          >
            <el-option label="不限" value=""></el-option>
            <el-option
              v-for="item in categorys"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleReset">重置</el-button>
          <el-button type="primary" @click="loadResources" :disabled="isLoading"
            >查询搜索</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 按钮 -->
    <el-button @click="handleDlg(0)">添加</el-button>
    <el-button @click="$router.push({ name: 'resource-category' })"
      >资源分类</el-button
    >
    <!-- 表格table -->
    <el-table
      :data="queryResult.records"
      border
      style="width: 100%"
      v-loading="isLoading"
    >
      <el-table-column type="index" label="编号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="name" label="资源名称" align="center">
      </el-table-column>
      <el-table-column prop="url" label="资源路径" align="center">
      </el-table-column>
      <el-table-column prop="description" label="描述" align="center">
      </el-table-column>
      <el-table-column
        prop="createdTime"
        label="添加时间"
        :formatter="timeFormatter"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template v-slot="scope">
          <el-button size="mini" @click="handleDlg(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.current"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryParams.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="queryResult.total"
      :disabled="isLoading"
    >
    </el-pagination>
    <resource-create-or-edit
      @resource-change="loadResources"
      ref="resourceDlg"
    />
  </el-card>
</template>
<script>
//1、导入接口函数
import { queryResource, deleteResource } from "@/api/resource";

//获取所有资源类别接口
import { getAllResourceCategory } from "@/api/resource-category";
import dayjs from "dayjs";
import ResourceCreateOrEdit from "./create-or-edit.vue";
export default {
  name: "ResourceIndex",
  data() {
    return {
      //3、查询条件
      queryParams: {
        id: undefined,
        name: undefined,
        startCreateTime: undefined,
        url: undefined,
        categoryId: "",
        endCreateTime: undefined,
        current: 1,
        size: 10,
      },
      //4、查询结果
      queryResult: {},
      //资源分类信息
      categorys: [],
      //数据是否加载中
      isLoading: false,
    };
  },
  created() {
    this.loadResources();
    this.loadCategory();
  },
  methods: {
    //2、封装方法，调接口(获取所有资源)
    loadResources() {
      this.isLoading = true;
      queryResource(this.queryParams)
        .then((res) => {
          if (res.data.code === "000000") {
            const { current, pages, records, size, total } = res.data.data;
            Object.assign(this.queryParams, { current, size });
            this.queryResult = { pages, records, total };
          } else {
            this.$message.error("获取资源数据失败~");
          }
          console.log("条件查询-所有资源信息", res);
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    //获取所有资源类别
    loadCategory() {
      getAllResourceCategory().then((res) => {
        if (res.data.code === "000000") {
          this.categorys = res.data.data;
        } else {
          this.$message.error("获取资源类别失败~");
        }
        console.log("资源类别111", res);
      });
    },
    //格式化时间
    timeFormatter(row, column, value, index) {
      return dayjs(value).format("YYYY-MM-DD");
    },
    //重置查询条件
    handleReset() {
      this.$refs.resourceForm.resetFields();
    },
    //当改变每页显示记录数时
    handleSizeChange(val) {
      this.queryParams.size = val;
      this.queryParams.current = 1;
      this.loadResources();
      // console.log(`每页 ${val} 条`);
    },
    //当点击不同的页号时
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.loadResources();
      // console.log(`当前页: ${val}`);
    },
    //添加或编辑的事件处理函数,呼出添加或编辑对话框
    handleDlg(resourceId) {
      this.$refs.resourceDlg.showAndInit(resourceId);
    },
    //删除某个资源
    handleDelete(resourceId) {
      this.$confirm("确认要删除该资源吗?", "删除提示")
        .then(() => {
          //确认删除
          deleteResource(resourceId).then((res) => {
            if (res.data.code === "000000") {
              this.$message.success("删除资源成功!");
              this.loadResources();
            } else {
              this.$message.error("删除资源失败~");
            }
          });
        })
        .catch(() => {
          this.$message.info("取消了删除资源的操作");
        });
    },
  },
  components: {
    ResourceCreateOrEdit,
  },
};
</script>
<style lang="scss" scoped>
.el-table {
  margin-top: 20px;
}
</style>
